package com.example.ivory.accountbook.Activity;

/**
 * Created by Ivory on 2016/8/26.
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ivory.accountbook.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class login extends AppCompatActivity implements View.OnClickListener {
    private EditText user;
    private String userString;
    private EditText pwd;
    private Button loginButton;
    private Button forget_pwd;
    private Button register;

    public void initView() {
        user = (EditText) findViewById(R.id.user);
        pwd = (EditText) findViewById(R.id.pwd);
        loginButton = (Button) findViewById(R.id.loginButton);
        forget_pwd = (Button) findViewById(R.id.forget_pwd);
        register = (Button) findViewById(R.id.register);
    }

    public void initEvents() {
        loginButton.setOnClickListener(this);
        forget_pwd.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        initView();
        initEvents();
    }



    public static boolean isMobileNumberValid(String mobiles) {
        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[1,3,5-8])|(14[5,7,9])|(18[0,2-9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return m.matches();
    }

    //验证是否为邮箱
    public static boolean isEmailValid(String email) {
        Pattern p = Pattern.compile("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
        Matcher m = p.matcher(email);
        return m.matches();
    }

    private void login() {
        userString = user.getText().toString();
        if (userString.isEmpty()) {
            Toast.makeText(login.this, "账号密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            if (isMobileNumberValid(userString) || isEmailValid(userString)) {
                //if（检查密码正确）{
                Intent main = new Intent(login.this, Activity_main.class);
                finish();
                startActivity(main);
                // }else{
                //Toast.makeText(login.this, "密码错误", Toast.LENGTH_SHORT).show();
                // }
            }else{
                Toast.makeText(login.this, "用户名错误", Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.loginButton:
                login();
                break;
            case R.id.forget_pwd:
                Toast.makeText(login.this, "forget", Toast.LENGTH_LONG).show();
                break;
            case R.id.register:
                Intent intent=new Intent(login.this,Sign.class);
                startActivity(intent);
                break;
        }
    }
}
